#include "StdAfx.h"
#include "parameters.h"
#include "GlobalConfig.h"
#include "Recorder.h"

#include <QtCore/QString>


parameters::parameters(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(Qt::WindowMinimizeButtonHint);

	connect(ui.browse,SIGNAL(clicked()),this,SLOT(browse()  ));
	connect(ui.begin ,SIGNAL(clicked()),this,SLOT(beginSim()));
	
}

parameters::~parameters()
{

}

void parameters::preShow()
{
	Situation* pCurrSituation = DataManager::getPreset(0);
	if (pCurrSituation == NULL) { return; }
	if (!pCurrSituation->isValid()) { return; }

	Situation& currSituation = *pCurrSituation;

	ui.cboOurAddr->setCurrentIndex(DataManager::findAddressIndex(currSituation.our.addr));
	ui.editOurAlt->setText(QString::number(currSituation.our.alt));
	ui.cboOurMissile->setCurrentIndex(DataManager::findOurMissileIndex(currSituation.our.missile));
	ui.editOurThetaEarth->setText(QString::number(currSituation.our.thetaEarth));
	ui.editOurThetaNorth->setText(QString::number(currSituation.our.thetaNorth));
	ui.editOurDelay->setText(QString::number(currSituation.our.delayTime));

	ui.cboEnemyAddr->setCurrentIndex(DataManager::findAddressIndex(currSituation.enemy.addr));
	ui.editEnemyAlt->setText(QString::number(currSituation.enemy.alt));
	ui.cboEnemyMissile->setCurrentIndex(DataManager::findEnemyMissileIndex(currSituation.enemy.missile));
	ui.editEnemyThetaEarth->setText(QString::number(currSituation.enemy.thetaEarth));
	ui.editEnemyThetaNorth->setText(QString::number(currSituation.enemy.thetaNorth));

	ui.editBlindRate->setText(QString::number(currSituation.infrared.blindRate));
	ui.editCameraAngle->setText(QString::number(currSituation.infrared.cameraAngle));
	ui.editResolution->setText(QString::number(currSituation.infrared.resolution));
	ui.editUpdateRate->setText(QString::number(currSituation.infrared.updateRate));

	Qt::CheckState state = currSituation.other.saveToFile ? Qt::Checked : Qt::Unchecked;
	ui.bSave->setCheckState(state);
	ui.initDistEdit->setText(QString::number(currSituation.other.distForStartInfr));
}

void parameters::browse()
{

	QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",QFileDialog::ShowDirsOnly
                                                                | QFileDialog::DontResolveSymlinks);
	ui.showPath->setText(dir);

}
void parameters::beginSim()
{
	Situation temp;

	Situation* pCurrSituation = DataManager::getCurrSituation();
	if (pCurrSituation == NULL)
	{
		pCurrSituation = &temp;
	}
	Situation& currSituation = *pCurrSituation;
	

	currSituation.our.addr			= DataManager::getAddress(ui.cboOurAddr->currentIndex());
	currSituation.our.alt			= ui.editOurAlt->text().toFloat();
	currSituation.our.missile		= DataManager::getOurMissile(ui.cboOurMissile->currentIndex());
	currSituation.our.thetaEarth	= ui.editOurThetaEarth->text().toFloat();
	currSituation.our.thetaNorth	= ui.editOurThetaNorth->text().toFloat();
	currSituation.our.delayTime 	= ui.editOurDelay->text().toFloat();

	currSituation.enemy.addr		= DataManager::getAddress(ui.cboEnemyAddr->currentIndex());
	currSituation.enemy.alt			= ui.editEnemyAlt->text().toFloat();
	currSituation.enemy.missile		= DataManager::getEnemyMissile(ui.cboEnemyMissile->currentIndex());
	currSituation.enemy.thetaEarth	= ui.editEnemyThetaEarth->text().toFloat();
	currSituation.enemy.thetaNorth	= ui.editEnemyThetaNorth->text().toFloat();

	currSituation.infrared.blindRate   = ui.editBlindRate->text().toFloat();
	currSituation.infrared.cameraAngle = ui.editCameraAngle->text().toFloat();
	currSituation.infrared.resolution  = ui.editResolution->text().toShort();
	currSituation.infrared.updateRate  = (byte)ui.editUpdateRate->text().toInt();

	currSituation.other.saveToFile = (ui.bSave->checkState() == Qt::Checked);
	currSituation.other.distForStartInfr = ui.initDistEdit->text().toFloat();


	DataManager::setCurrSituation(0);
	Recorder::inst()->getReady();

	accept();
}
